package it.mauriziobasile.andoid.aub;

import it.mauriziobasile.andoid.aub.model.Hosts;
import it.mauriziobasile.andoid.aub.net.AsynchProcessListener;
import it.mauriziobasile.andoid.aub.net.HostDefinitionDownloader;
import it.mauriziobasile.andoid.aub.runnable.Backup;
import it.mauriziobasile.andoid.aub.runnable.Restore;
import it.mauriziobasile.andoid.aub.utils.Logger;

import org.apache.http.ParseException;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class AdvUniversalBlockerActivity extends Activity {

    private static final String HOSTS_DEFINITION_URL = "http://adv-universal-blocker.googlecode.com/svn/trunk/support/hosts.xml";
    public static final String MSG_ACTION = "it.mauriziobasile.andoid.aub.action.sendmsg";

    private TextView notification;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        registerReceiver(msgReceiver, new IntentFilter(MSG_ACTION));
        createListener();
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(msgReceiver);
        super.onDestroy();
    }

    private void createListener() {
        findViewById(R.id.download_and_update).setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                startDownloadAndUpdate();
            }
        });
        findViewById(R.id.backup_orginal).setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                new Thread(new Backup(AdvUniversalBlockerActivity.this)).start();
            }
        });
        findViewById(R.id.restore_orginal).setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                new Thread(new Restore(AdvUniversalBlockerActivity.this)).start();
            }
        });

    }

    private void startDownloadAndUpdate() {

        HostDefinitionDownloader.getInstance().getHostsDefinitions(HOSTS_DEFINITION_URL, new AsynchProcessListener() {

            public void onProgress(int step) {
            }

            public void onProgress(float percent) {
            }

            public void onFinishDownload(Object result) {
                try {
                    Hosts hosts = (Hosts) result;
                    Logger.d("VERSION IS: " + hosts.getMeta().getVersion());
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

            public void onError(int errCode, String message) {
                Logger.e("ERRORE: (" + errCode + ")->" + message);
            }

            public void onBeginDownload() {
            }
        }, this);
    }

    BroadcastReceiver msgReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String msg = intent.getStringExtra("msg");
            if (msg != null) {
                if (notification == null) {
                    notification = (TextView) findViewById(R.id.notification);
                }
                notification.setText(msg);
            }
        }
    };
}